﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Web;
using NhanAi.DAO;
using NhanAi.Entity;
using Home.Utils;
namespace NhanAi.DAO.Mix
{

    public class PostModel
    {
        PostDao daoPost = new PostDao();
        TaxonomyDao daoTax = new TaxonomyDao();
        public void Create(Post p)
        {
            TaxonomyRelationshipDao daoRel = new TaxonomyRelationshipDao();
            daoPost.Create(p);
            if (p.Taxonomies != null)
                foreach (Taxonomy t in p.Taxonomies)
                {
                    daoRel.Create(p.Id, t.Id);
                }
        }
        public void Update(Post p)
        {
            TaxonomyRelationshipDao daoRel = new TaxonomyRelationshipDao();
            daoPost.Update(p);
            daoRel.Delete(p.Id);
            if (p.Taxonomies != null)
                foreach (Taxonomy t in p.Taxonomies)
                {
                    daoRel.Create(p.Id, t.Id);
                }
        }
        public void UpdateViewCount(int id, int quantity)
        {
            try
            {
                daoPost.UpdateViewCount(id, quantity);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void UpdateCommentCount(int id, int quantity)
        {
            try
            {
                daoPost.UpdateCommentCount(id, quantity);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        public void Delete(int id)
        {
            daoPost.Delele(id);
        }
        public void Delete(string ids)
        {
            daoPost.Delete(ids);
        }
        public Post Read(int id, int lang)
        {
            Post p = daoPost.Read(id, lang);
            List<Taxonomy> taxs = daoTax.ReadByPost(id);
            p.Taxonomies = taxs; ;
            return p;
        }
        public List<Post> ReadMany(int pageSize, int pageNum, int lang, string type, string text, bool fullSearch, int attribute, int status, DateTime? fromDate, DateTime? toDate, out int total)
        {
            List<Post> list = daoPost.ReadMany(pageSize, pageNum, lang, type, text, fullSearch, attribute, status, fromDate, toDate, out total);
            foreach (Post p in list)
            {
                p.Taxonomies = daoTax.ReadByPost(p.Id);
            }
            return list;
        }
        public List<Post> ReadByTaxonomy(int pageSize, int pageNum, int lang, string type, int taxonomyId, string text, bool fullSearch, int attribute, int status, DateTime? fromDate, DateTime? toDate, out int total)
        {
            List<Post> list = daoPost.ReadByTaxonomy(pageSize, pageNum, lang, type, taxonomyId, text, fullSearch, attribute, status, fromDate, toDate, out total);
            foreach (Post p in list)
            {
                p.Taxonomies = daoTax.ReadByPost(p.Id);
            }
            return list;
        }
        public List<Post> ReadAdv(int pageSize, int pageNum, int lang, string type, string text, int status, out int total)
        {
            return daoPost.ReadAdv(pageSize, pageNum, lang, type, text, status, out total);
        }
    }
}